package com.saatcioglu.android.guessthenumber;

import android.app.Activity;
import android.graphics.Color;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class NumberConsole{
	static final protected int NOTSURE_ID = Menu.FIRST + 11;
	static final protected int DEFINITELY_ID = Menu.FIRST + 12;
	static final protected int MAYBE_ID = Menu.FIRST + 13;
	static final protected int NOTACHANCE_ID = Menu.FIRST + 14;

	private Button btnClear;
	public Button btnEnter;
	public Button[] arrbtnNumbers = new Button[10];
	private int theDigitsPressed;
	private int[] arrintPrevButtons;
	private Button mSelectedButton;
	
	public NumberConsole(Activity view){
		arrbtnNumbers[0] = (Button) view.findViewById(R.id.number_zero);
		arrbtnNumbers[1] = (Button) view.findViewById(R.id.number_one);
		arrbtnNumbers[2] = (Button) view.findViewById(R.id.number_two);
		arrbtnNumbers[3] = (Button) view.findViewById(R.id.number_three);
		arrbtnNumbers[4] = (Button) view.findViewById(R.id.number_four);
		arrbtnNumbers[5] = (Button) view.findViewById(R.id.number_five);
		arrbtnNumbers[6] = (Button) view.findViewById(R.id.number_six);
		arrbtnNumbers[7] = (Button) view.findViewById(R.id.number_seven);
		arrbtnNumbers[8] = (Button) view.findViewById(R.id.number_eight);
		arrbtnNumbers[9] = (Button) view.findViewById(R.id.number_nine);
		btnEnter = (Button) view.findViewById(R.id.enter);
		btnClear = (Button) view.findViewById(R.id.clear);
		for (int i = 0; i < 10; i++) {
			arrbtnNumbers[i].setOnClickListener((OnClickListener) view);
			view.registerForContextMenu(arrbtnNumbers[i]);
		}
		btnEnter.setOnClickListener((OnClickListener) view);
		btnClear.setOnClickListener((OnClickListener) view);
	}

	public void reset(){
		for (int i = 1; i < 10; i++) {
			arrbtnNumbers[i].setEnabled(true);
		}
		arrbtnNumbers[0].setEnabled(false);
		btnEnter.setEnabled(false);
		btnClear.setEnabled(false);
		for (int i = 0; i < 10; i++) {
			arrbtnNumbers[i].setTextColor(Color.BLACK);
			SMBGuesstheNumber.arrintBtnNumberStates[i] = 0;
		}
		arrintPrevButtons = new int[SMBGuesstheNumber.nActiveDigits];
		theDigitsPressed = 0;
	}
	
	public void disable(){
		for (int i = 0; i < 10; i++) {
			arrbtnNumbers[i].setEnabled(false);
		}
		btnEnter.setEnabled(false);
		btnClear.setEnabled(false);
	}
	
	public void numberSelected(EditText theNumber, Button theButtonPressed){
		theButtonPressed.setEnabled(false);
		arrintPrevButtons[theDigitsPressed++] = Integer.parseInt(theButtonPressed.getText().toString());

		if (theNumber.getText().toString().length() == 1) {
			arrbtnNumbers[0].setEnabled(true);
			btnClear.setEnabled(true);
		}
		if (theNumber.getText().toString().length() == SMBGuesstheNumber.nActiveDigits) {
			for (int i = 0; i < 10; i++) {
				arrbtnNumbers[i].setEnabled(false);
			}
			btnEnter.setEnabled(true);
		}
	}
	
	public void clearNumber(EditText edtNewNumber){
		if (edtNewNumber.getText().toString().length() == SMBGuesstheNumber.nActiveDigits - 1) {
			for (int i = 0; i < 10; i++) {
				arrbtnNumbers[i].setEnabled(true);
			}
			theDigitsPressed--;
			for (int i = 0; i < theDigitsPressed; i++) {
				arrbtnNumbers[arrintPrevButtons[i]].setEnabled(false);
			}
			btnEnter.setEnabled(false);
		} else {
			arrbtnNumbers[arrintPrevButtons[--theDigitsPressed]]
					.setEnabled(true);
		}

		if (edtNewNumber.getText().toString().length() == 0) {
			arrbtnNumbers[0].setEnabled(false);
			btnClear.setEnabled(false);
		}
	}
	
	public void enterNumber(){
		for (int i = 1; i < 10; i++) {
			arrbtnNumbers[i].setEnabled(true);
		}
		arrbtnNumbers[0].setEnabled(false);
		theDigitsPressed = 0;
		btnEnter.setEnabled(false);
		btnClear.setEnabled(false);
	}

	public void update(){
		if (!SMBGuesstheNumber.bDisplayFlagList) {
			for (int i = 0; i < 10; i++) {
				arrbtnNumbers[i].setTextColor(Color.BLACK);
			}
		}
	}
	
	public void resume(){
		theDigitsPressed = SMBGuesstheNumber.theDigitsPressed;
		arrintPrevButtons = SMBGuesstheNumber.arrintPrevButtons;

		for (int i = 0; i < 10; i++) {
			arrbtnNumbers[i]
					.setEnabled(SMBGuesstheNumber.arrisBtnNumberEnabled[i]);
			switch (SMBGuesstheNumber.arrintBtnNumberStates[i]) {
			case 0:
				arrbtnNumbers[i].setTextColor(Color.BLACK);
				break;
			case 1:
				arrbtnNumbers[i].setTextColor(Color.GREEN);
				break;
			case 2:
				arrbtnNumbers[i].setTextColor(Color.YELLOW);
				break;
			case 3:
				arrbtnNumbers[i].setTextColor(Color.RED);
				break;
			}
		}
		btnClear.setEnabled(SMBGuesstheNumber.arrisBtnNumberEnabled[10]);
		btnEnter.setEnabled(SMBGuesstheNumber.arrisBtnNumberEnabled[11]);
	}
	
	public void saveTheState(){
		SMBGuesstheNumber.theDigitsPressed = theDigitsPressed;
		SMBGuesstheNumber.arrintPrevButtons = arrintPrevButtons;
		for (int i = 0; i < 10; i++) {
			SMBGuesstheNumber.arrisBtnNumberEnabled[i] = arrbtnNumbers[i]
					.isEnabled();
		}
		SMBGuesstheNumber.arrisBtnNumberEnabled[10] = btnClear.isEnabled();
		SMBGuesstheNumber.arrisBtnNumberEnabled[11] = btnEnter.isEnabled();
	}
	
	public ContextMenu createContextMenu(ContextMenu menu, Button button){
		menu.setHeaderIcon(R.drawable.droid);
		menu.setHeaderTitle(R.string.contextmenu_flaglist);
		menu.add(0, NOTSURE_ID, 0, R.string.contextmenu_flaglist_notsure);
		menu.add(0, DEFINITELY_ID, 0, R.string.contextmenu_flaglist_definitely);
		menu.add(0, MAYBE_ID, 0, R.string.contextmenu_flaglist_maybe);
		menu.add(0, NOTACHANCE_ID, 0, R.string.contextmenu_flaglist_notachance);
		mSelectedButton = button;
		return menu;
	}
	
	public boolean doContextMenu(MenuItem item){
		SMBGuesstheNumber.arrintBtnNumberStates[Integer.parseInt(mSelectedButton.getText().toString())] 
		                                        = item.getItemId() - NOTSURE_ID;
		switch (item.getItemId()) {
		case NOTSURE_ID:
			mSelectedButton.setTextColor(Color.BLACK);
			return true;
		case DEFINITELY_ID:
			mSelectedButton.setTextColor(Color.GREEN);
			return true;
		case MAYBE_ID:
			mSelectedButton.setTextColor(Color.YELLOW);
			return true;
		case NOTACHANCE_ID:
			mSelectedButton.setTextColor(Color.RED);
			return true;
		}
		return false;
	}
}
